package com.beehoo.dcportal.utils;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beehoo.dcportal.common.Constant;

public class AdminFilter implements Filter {
	private String sessionStreetId = Constant.SESSION_STREET_ID;
	private String sessionUserId = Constant.SESSION_USER_ID;
	private String errorPage = null;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();
		
		String requestURI = req.getContextPath();

		String userId = CommonUtils.nvlToString((String) session
				.getAttribute(sessionUserId));
		String streetId = CommonUtils.nvlToString((String) session
				.getAttribute(sessionStreetId));

		if (userId.equals("") || streetId.equals("")) {
			res.sendRedirect(requestURI+errorPage); 
		} else {
			chain.doFilter(request, response);
		}
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		errorPage = filterConfig.getInitParameter("errorPage");
	}

}
